<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>冒泡排序</title>
</head>
<body>
    <script>
        let array = [1,3,43,2,5,14,23,7];
        //两个for循环,只要大小不是顺序排列就交换,优化:定义一个布尔类型,如果这一趟没有发生交换就结束整个循环,表示这个数组已经排好序了
        for(let i = 0;i < array.length - 1;i ++){
            let f = true;
            for(let j = 0;j < array.length - 1- i;j ++){
                if(array[j] > array[j + 1]){
                    let tmp = array[j];
                    array[j] = array[j + 1];
                    array[j + 1] = tmp;
                    f = false;
                }
            }
            if(f){
                break;
            }
        } 
        console.log(array);
    </script>
</body>
</html>